var t = getApp()
Page({
  data: {
    site:'',
    store:'',
    ismini:0, //是否是小程序入场
    info:'',
    user_id:'',
    coupon_num:0,
    balance:0,
    describe:'',
    setInter:'',
    getInfo:'',
    store_info:'',
    navHeight:t.globalData.navHeight,
    // 获取手机号弹窗参数
    show: false,
    duration: 300, //动画时长，单位毫秒
    position: 'bottom',
    round: true,  //是否显示圆角
    overlay: true, //是否显示遮罩层
    customStyle: '',//自定义弹出层样式
    overlayStyle: '', //自定义遮罩层样式
    //  车牌
    is_parking:0, //是否是特殊店铺（洛阳店）
    showNewPower: false,
    KeyboardState: false,
    phone_type:'',
    name:'',
    tel:'',
    carnum: [],
    carnumstr:'',
    lastCarNum:wx.getStorageSync('lastCarNum'),
    provinces: [
      ['京', '沪', '粤', '津', '冀', '晋', '蒙', '辽'],
      ['新', '吉', '黑', '苏', '浙', '皖', '闽', '赣'],
      [ '鲁', '宁','豫', '鄂', '湘' ,'桂', '琼', '渝'],
      ['川', '贵', '青', '云', '藏','陕', '甘'],
    ],
    letters:[
      ["A", "B", "C", "D", "E", "F","G", "H"],
      ["J", "K", "L", "M", "N", "P","Q", "R"],
      ["S", "T", "U", "V", "W", "X","Y", "Z"],
    ],
    numbers: [
      ["1", "2", "3", "4", "5", "6", "7", "8"],
      ["9", "0", "A", "B", "C", "D", "E", "F"],
      ["G", "H", "J", "K", "L", "M", "N", "P"],
      ["Q", "R", "S", "T", "U", "V", "W", "X"],
      ["Y", "Z", "港", "澳", "警" ,"学", "领"]
    ],
  },
  onLoad: function (options) {
    console.log(options);
    var that = this
    if (options) {
      this.setData({
        site:options.site,
        store:options.store,
        ismini:options.ismini,
      })
    }else{
      wx.switchTab({
        url: '/pages/index/index',
      })
      return
    }
  },
  onShow(e){
    this.setData({
      lastCarNum :wx.getStorageSync('lastCarNum')
    })
    var that = this
    wx.login({
      success: res => {
        t.request('/login',{code:res.code},function(r){
          console.log(r);
            t.request('/users/get_store_balance',{store:that.data.store,site:that.data.site},function(e){
              console.log(e);
              if (e.code==200) {
                that.setData({
                  balance:e.result.balance,
                  store_info:e.result.store,
                  coupon_num:e.result.coupon_num,
                  describe:e.result.describe,
                  user_id:e.result.user_id,
                  is_parking:e.result.is_parking
                })
              }else if(e.code==400){
								wx.showModal({
									title: e.msg,
									content: '',
									complete: (res) => {
										if (res.cancel) {
											wx.switchTab({
												url: '/pages/index/index',
											})
										}
								
										if (res.confirm) {
											wx.switchTab({
												url: '/pages/index/index',
											})
										}
									}
								})
              }
            })
          // 先判断是否绑定电话
          if(r.result.userInfo.get_phone !=1){
            t.showTelLogin()
          }
          // 判断是否有车辆
          if (r.result.userInfo.cars<1) {
            // that.login = that.selectComponent("#login")
            // that.login.show_login()
          }
        })
      }
    })
  },
//  充值
  invest(e){
    var that = this
    wx.navigateTo({
      url: '/pages/personCenter/chooseStore/invest/invest?store_id=' + that.data.store,
    })
  },
  // 点击入场
  open_gate(e){
    var that = this
    // 洛阳店特殊处理
    if(that.data.is_parking){
      if(that.data.carnumstr==''){
        that.add_car()
        return
      }else{
        if (that.data.showNewPower) {
          if (that.data.carnumstr.length!==8){
            wx.showToast({
              title: '请输入正确的新能源车牌号',
              icon:'none'
            })   
            return;
          }
        }else{
          if (that.data.carnumstr.length!==7){
            wx.showToast({
              title: '请输入正确的车牌号',
              icon:'none'
            })   
            return;
          }
        }
      }
    }
    wx.showLoading({title: '请稍后',})
    let obj ={
      store:that.data.store,
      site:that.data.site,
      is_mini:that.data.ismini,
      parking_code:that.data.carnumstr
    }
    t.request('/StoreSite/start_order',obj,function(e){
      console.log(e);
      wx.hideLoading({success: (res) => {},})
      if (e.code==200) {
        wx.showToast({
          title: e.msg,
        })
        wx.switchTab({
          url: '/pages/index/index',
        })
      }else{
        wx.showToast({
          title: e.msg,
          icon:'none'
        })
      }
    })
  },
  // 使用上次车牌
  addLastNum(){
    this.setData({
      carnumstr :this.data.lastCarNum,
      showNewPower:this.data.lastCarNum.length==8?true:false
    })
  },
  // 添加车辆
  add_car(e){
    this.setData({
      show:true,
      KeyboardState:true,
      choose_num:this.data.carnum.length
    })
  },
  // 添加车辆提交
  submit_add_car(e){
    var that = this
    var carnum = this.data.carnum.toString().replace(/,/g, "")
    console.log(this.data.carnum);
    if (this.data.showNewPower) {
      if (carnum.length!==8){
        wx.showToast({
          title: '请输入正确的新能源车牌号',
          icon:'none'
        })   
        return;
      }
    }else{
      if (carnum.length!==7){
        wx.showToast({
          title: '请输入正确的车牌号',
          icon:'none'
        })   
        return;
      }
    }
    wx.showModal({
      title:'提示',
      content:'确认车牌是否准确？',
      success(e){
        if (e.confirm) {
          wx.setStorageSync('lastCarNum', that.data.carnumstr)
          that.exit()
        }
      }
    })
    
  },
  // 退出车辆添加
  exit() {
      this.setData({
      show:false,
      KeyboardState: false,
      choose_num:null,
    })
  },
  bindChoose(e) {
    if (this.data.carnum.length<7 || (this.data.showNewPower && this.data.carnum.length<8)) {
      var arr = [];
      arr[0] = e.target.dataset.val;
      this.data.carnum = this.data.carnum.concat(arr)
      this.setData({
        carnum: this.data.carnum,
        carnumstr:this.data.carnum.toString().replace(/,/g, ""),
        choose_num:this.data.carnum.length
      })
    }
  },
  bindDelChoose() {
    if (this.data.carnum.length != 0) {
      this.data.carnum.splice(this.data.carnum.length - 1, 1);
      this.setData({
        carnum: this.data.carnum,
        carnumstr:this.data.carnum.toString().replace(/,/g, ""),
        choose_num:this.data.carnum.length
      })
    }
  },
  // 燃油、新能源 切换
  showPowerBtn(e) {
    console.log(e.detail.value)
    this.setData({
      showNewPower: !this.data.showNewPower,
      KeyboardState: true,
      carnum:[],
      choose_num:0
    })
  },

})